/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.srdxeditor.dialogs;

import net.miblounge.gradecalculator.core.descriptors.modulelistitems.Module;
import net.miblounge.gradecalculator.guicommon.dialogs.AbstractDialog;
import net.miblounge.gradecalculator.guicommon.layouts.VerySimpleLayout;
import net.miblounge.gradecalculator.guicommon.util.StringUtil;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QWidget;

public class ModuleDialog extends AbstractDialog {
	private QLineEdit leName;
	
	private final Module module;

	public ModuleDialog(final QWidget parent, final Module module)
	{
		super(parent);
		setWindowTitle(tr("Module"));
		
		this.module = module;
		
		setClientArea(createClientArea());

		leName.setFocus();
		leName.setSelection(0, leName.text().length());
		
		revalidate();
	}
	
	private QGroupBox createClientArea()
	{
		final QGroupBox group = new QGroupBox(tr("Module"), this);
		final VerySimpleLayout layoutGroup = new VerySimpleLayout(group);
		group.setLayout(layoutGroup);

		final QWidget container = new QWidget(group);
		layoutGroup.setWidget(container);
		final MigLayout gbLayout = new MigLayout("", "[shrink 0][grow]", ""); 
		container.setLayout(gbLayout);
	
		gbLayout.addWidget(new QLabel(tr("Name:")));
		leName = new QLineEdit(module.getName(), this);
		leName.textEdited.connect(this, "revalidate()");
		gbLayout.addWidget(leName, "w 50:200:400, growx");

		return group;
	}
	
	@Override
	protected boolean validate()
	{
		if (StringUtil.cleanString(leName.text()).equals("")) {
			return false;
		}

		return super.validate();
	}
	
	@Override
	public void apply()
	{
		super.apply();
		module.setName(StringUtil.cleanString(leName.text()));
	}

}
